#include<formic/output/output.h>
#include<formic/mpi/interface.h>

formic::Output<std::ofstream> formic::of;

void formic::open_output_file(const std::string & filename) {

  if ( formic::mpi::rank() != 0 )
    return;

  boost::shared_ptr<std::ofstream> ptr( new std::ofstream(filename.c_str(), std::ios_base::out | std::ios_base::trunc) );
  if ( !ptr->is_open() )
    throw formic::Exception("Failed to open file \"%s\" in formic::open_output_file") % filename;

  formic::Output<std::ofstream> temp_of(ptr);
  formic::of = temp_of;
  temp_of = formic::Output<std::ofstream>();

}

void formic::close_output_file() {

  if ( formic::mpi::rank() != 0 )
    return;

  formic::of = formic::Output<std::ofstream>();

}
